package com.bw.quickadapter;

import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.glide_sdk.ImgLoad;


public class BaseViewHolder extends RecyclerView.ViewHolder {

    private SparseArray<View>sparseArray=new SparseArray<>();//键值对类型键为int值为object 底层使用int以及object数组实现

    public BaseViewHolder(@NonNull View itemView) {
        super(itemView);
    }
    private <V extends View> V find(int id){
        return itemView.findViewById(id);
    }
    public <V extends View> V getView(int id){
        V v;
        v = (V) sparseArray.get(id);
        if (v!=null)
            return v;

        v = find(id);
        if (v!=null)
            sparseArray.put(id, v);
        return v;
    }

    public void setText(int id,String content){
        if (TextUtils.isEmpty(content))
            return;
        TextView textView = getView(id);
        if (textView == null)
            return;
        textView.setText(content);
    }

    //设置控件显示隐藏
    public void setVisibility(int id,boolean visibilityFlag){
        View view = getView(id);
        if (view == null)
            return;
        view.setVisibility(visibilityFlag?View.VISIBLE:View.GONE);
    }

    //设置ImageView显示网络图片
    public void setImage(int id,String path){
        if (TextUtils.isEmpty(path))
            return;
        ImageView imageView = getView(id);
        if (imageView == null)
            return;
        //调用封装完成的glide加载网络图片
        ImgLoad.Companion.loadMatchImg(path,imageView);
    }

    //设置ImageView显示Resource图片
    public void setImage(int id,int resId){
        ImageView imageView = getView(id);
        if (imageView == null)
            return;
        imageView.setImageResource(resId);
        //调用封装完成的glide加载网络图片
//        ImgLoad.Companion.loadResImg(resId,imageView);
    }

}
